<?php
require_once("Includes/db.php");

/**other variables */
$userNameIsUnique = true;
$passwordIsValid = true;
$userIsEmpty = false;
$passwordIsEmpty = false;
$password2IsEmpty = false;
$streetIsEmpty = false;
$cityIsEmpty = false;
$stateIsEmpty = false;
$countryIsEmpty = false;
$zipIsEmpty = false;

/** Check that the page was requested from itself via the POST method. */
if ($_SERVER['REQUEST_METHOD'] == "POST"){
    /** Check whether the user has filled in the wisher's name in the text field "user" */
    if ($_POST['user']==""){
        $userIsEmpty = true;
    }

    /** Create database connection */

    $customerID = icsDB::getInstance()->get_customer_id_by_name($_POST['user']);
    if ($customerID) {
        $userNameIsUnique = false;
    }

    /** Check whether a password was entered and confirmed correctly */
    if ($_POST['password']=="")
    $passwordIsEmpty = true;
    if ($_POST['password2']=="")
    $password2IsEmpty = true;
    if ($_POST['password']!=$_POST['password2']) {
        $passwordIsValid = false;
    if ($_POST['street']=="")
        $streetIsEmpty = true;
    if ($_POST['city']=="")
        $cityIsEmpty = true;
    if ($_POST['state']=="")
        $stateIsEmpty = true;
    if ($_POST['country']=="")
        $countryIsEmpty = true;
    if ($_POST['zip']=="")
        $zipIsEmpty = true;

        
    }

    /** Check whether the boolean values show that the input data was validated successfully.
     * If the data was validated successfully, add it as a new entry in the "wishers" database.
     * After adding the new entry, close the connection and redirect the application to editWishList.php.
     */
    if (!$userIsEmpty && $userNameIsUnique && !$passwordIsEmpty && !$password2IsEmpty && $passwordIsValid) {
    	icsDB::getInstance()->create_customer($_POST['user'], $_POST['password'], $_POST['street'], $_POST['city'], $_POST['state'], $_POST['country'], $_POST['zip']);
    	session_start();
    	$_SESSION['user'] = $_POST['user'];
    	// header('Location: editWishList.php' ); /*not going to editWishList.php*/
    	exit;
    	// el comment
    }
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Order Entry application</title>
        <link href="ics.css" type="text/css" rel="stylesheet" media="all" />
    </head>
    <body>
        <h1>Welcome!</h1>
        
        <form action="addCustomer.php" method="POST" id="addUser">
            <label>Your name:</label>
            <input type="text" name="user"/><br/>
            <?php
            /** Display error messages if "user" field is empty or there is already a user with that name*/
            if ($userIsEmpty) {
                echo ('<div class="error">Please Enter your name</div>');
            }
            if (!$userNameIsUnique) {
                echo ('<div class="error">The person already exists. Please check the spelling and try again</div>');
            }
            ?>
            <label>Password:</label>
            <input type="password" name="password"/><br/>
            <?php
             /** Display error messages if the "password" field is empty */
            if ($passwordIsEmpty) {
                echo ('<div class="error">Enter the password, please</div>');
            }
            ?>
            <label>Password (Again):</label>
            <input type="password" name="password2"/><br/>
/* new add**/
            <label>street:</label>
            <input type="text" name="street"/><br/>
            <?php
             /** Display error messages if the "password" field is empty */
            if ($streetIsEmpty) {
                echo ('<div class="error"Please enter street name</div>');
            }
            ?>
            <label>City:</label>
            <input type="text" name="city"/><br/>
            <?php
             /** Display error messages if the "password" field is empty */
            if ($cityIsEmpty) {
                echo ('<div class="error">Please enter city name</div>');
            }
            ?>
            <label>Province/State:</label>
            <input type="text" name="state"/><br/>
            <?php
             /** Display error messages if the "password" field is empty */
            if ($stateIsEmpty) {
                echo ('<div class="error">Please enter name of province/state:</div>');
            }
            ?>
            <label>Country:</label>
            <input type="text" name="country"/><br/>
            <?php
             /** Display error messages if the "password" field is empty */
            if ($countryIsEmpty) {
                echo ('<div class="error">Please enter country name</div>');
            }
            ?>
            <label>Postal code:</label>
            <input type="text" name="zip"/><br/>
            <?php
             /** Display error messages if the "password" field is empty */
            if ($zipIsEmpty) {
                echo ('<div class="error">Please enter postal code</div>');
            }
            ?>
/* end add*/
            <?php
            /**
             * Display error messages if the "password2" field is empty
             * or its contents do not match the "password" field
             */
            if ($password2IsEmpty) {
                echo ('<div class="error">Confirm your password, please</div>');
            }
            if (!$password2IsEmpty && !$passwordIsValid) {
                echo ('<div class="error">Your passwords do not match.</div>');
            }
            ?>
            <br />
            <input type="submit" value="Register"/>

        </form>

    </body>
</html>